# -*- coding:utf-8 -*-
import logging

# 创建json串
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    def __init__(self,driver:WebDriver=None):
        self.driver=driver

    def slide(self,a,b):
        """
        从某个坐标点按住滑动再释放
        :return:
        """
        action = TouchAction(self.driver)
        window_rec = self.driver.get_window_size()  # 获取宽，高
        width = window_rec['width']  # 宽
        height = window_rec['height']  # 高
        x1 = int(width / 2)
        y_start = int(height * a)
        y_end = int(height * b)
        # 从某个坐标点按住滑动再释放
        return  action.press(x=x1, y=y_start).wait(100).move_to(x=x1, y=y_end).release().perform()

    def get_action(self,locator,x,y):
        """
        从某个坐标点按住滑动再释放
        :return:
        """
        action = TouchAction(self.driver)
        ele=self.driver.find_element_by_xpath(locator)
        return action.move_to(ele,x,y)

    def find_id(self,value):
        """
        添加显式等待
        通过id定位元素
        :param by:
        :param value:
        :return:
        """
        logging.info("find_id")
        logging.info(f'{value}')
        WebDriverWait(self.driver,10).until(expected_conditions.visibility_of_element_located((MobileBy.ID,value
                                                                                               )))
        return self.driver.find_element_by_id(value)

    def find_name(self,value):
        """
        添加显式等待
        通过id定位元素
        :param by:
        :param value:
        :return:
        """
        logging.info("find_name")
        logging.info(f'{value}')
        WebDriverWait(self.driver,10).until(expected_conditions.visibility_of_element_located((MobileBy.NAME,value)))
        return self.driver.find_element_by_name(value)
    def find_xpath(self,value):
        """
        添加显式等待
        通过xpath定位元素
        :param by:
        :param value:
        :return:
        """
        logging.info("find_xpath")
        logging.info(f'{value}')
        WebDriverWait(self.driver,10).until(expected_conditions.visibility_of_element_located((MobileBy.XPATH,value)))
        return self.driver.find_element_by_xpath(value)

    def find_xpath_send(self,value,text):
        """
        通过xpath定位元素并输入
        :param by:
        :param value:
        :param text:
        :return:
        """
        logging.info("find_xpath_send")
        logging.info(f'{value}')
        return self.find_xpath(value).send_keys(text)

    def find_id_send(self,value,text):
        """
        通过id定位元素并输入
        :param by:
        :param value:
        :param text:
        :return:
        """
        logging.info("find_id_send")
        logging.info(f'{value}')
        return self.find_id(value).send_keys(text)

    def find_xpath_click(self,value):
        """
        通过xpath定位元素并点击
        :param by:
        :param value:
        :return:
        """
        logging.info("find_xpath_click")
        logging.info(f'{value}')
        return  self.find_xpath(value).click()

    def find_id_click(self,value):
        """
        通过id定位元素并点击
        :param by:
        :param value:
        :return:
        """
        logging.info("find_id_click")
        logging.info(f'{value}')
        return  self.find_id(value).click()

    def back(self,num=3):
        """
        返回操作
        :param num:
        :return:
        """
        for i in range(0,num):
            self.driver.back()
            logging.info("返回操作")
    def find_uiautomator(self,text):
        """
         滑动查询某个元素
        :param text:
        :return:
        """
        logging.info(f'滑动查询：{text}')
        a = f'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("{text}").instance(0))'
        return self.driver.find_element_by_android_uiautomator(a)

    def find_uiautomator_click(self,text):
        """
         滑动查询某个元素,点击
        :param text:
        :return:
        """
        logging.info(f'滑动查询：{text},点击')
        a = f'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("{text}").instance(0))'
        return self.driver.find_element_by_android_uiautomator(a).click()

